package com.wjpeng.wms.web.controller;

import com.wjpeng.wms.domain.Employee;
import com.wjpeng.wms.query.EmployeeQueryObject;
import com.wjpeng.wms.service.IDepartmentService;
import com.wjpeng.wms.service.IEmployeeService;
import com.wjpeng.wms.service.IRoleService;
import com.wjpeng.wms.util.JSONResult;
import com.wjpeng.wms.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;

    @RequestMapping("list")
    @RequiredPermission("员工列表")
    public String list(@ModelAttribute("qo") EmployeeQueryObject qo, Model model){
        model.addAttribute("result",employeeService.result(qo));
        model.addAttribute("depts",departmentService.list());

        return "employee/list";
    }

    @RequestMapping("input")
    @RequiredPermission("员工编辑")
    public String input(Model model,Long id){
        if(id != null){
            model.addAttribute("entity",employeeService.get(id));
        }
        model.addAttribute("depts",departmentService.list());
        model.addAttribute("roles",roleService.list());
        return "employee/input";
    }

    @RequestMapping("delete")
    @ResponseBody
    @RequiredPermission("员工删除")
    public Object delete(Long id) throws  Exception{
         employeeService.delete(id);
        return new JSONResult();//象征性返回
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @RequiredPermission("员工新增/更新")
    public Object saveOrUpdate(Employee employee,Long[] roleIds){
        employeeService.saveOrUpdate(employee,roleIds);
        return new JSONResult();
    }

}
